import React from "react";
import Qs from 'qs'
import axios from "../../../http";
import {NavBar, Toast} from "antd-mobile";
import {
    Form,
    Input,
    Button,
} from 'antd-mobile';


import {withRouter} from "react-router";

class Profile extends React.Component {


    constructor(props) {

        super(props);

        this.state={};

    }
    componentWillUnmount() {

    }

    componentDidMount() {
        this.loadUser();
    }

    loadUser=()=>{
        axios.post('api/core/access/user/getLoginUser', Qs.stringify({

        }),{headers: {'Content-Type': 'application/x-www-form-urlencoded'}})
            .then((response)=>{
                let  data=response.data.data;
                this.setState(data)
            })
            .catch(function (error) {
                Toast.show({
                    icon: 'fail',
                    content: error,
                });
            });
    }

    updateProfile=()=>{
        let entity={
            "realName":this.state.realName,
            "email": this.state.email,
            "phone":this.state.phone,
            "mobilePhone":this.state.mobilePhone,
            "company":this.state.company
        }
        axios.post('api/core/access/user/updateProfile', Qs.stringify({
                requestJson:JSON.stringify(entity)
            }
        ),{headers: {'Content-Type': 'application/x-www-form-urlencoded'}})
            .then((response)=>{
                let  data=response.data;
                let icon= 'success';
                if(data.status!=1){
                    icon= 'fail';
                }
                Toast.show({
                    icon: icon,
                    content: data.message,
                });
            })
            .catch(function (error) {
                Toast.show({
                    icon: 'fail',
                    content: error,
                });
            });
    }

    render(){
        return(
            <>
                <NavBar onBack={()=>{this.props.history.push({ pathname: "/me" })}} >个人资料</NavBar>

                <Form
                    footer={
                        <Button block type='submit' color='primary' onClick={this.updateProfile}>
                            提交
                        </Button>
                    }
                >
                    <Form.Item
                        label='用户名：'
                    >
                        {this.state.name}
                    </Form.Item>
                    <Form.Item
                        label='真实姓名：'
                        rules={[{ required: true, message: '真实姓名不能为空' }]}
                    >
                        <Input placeholder='请输入真实姓名' value={this.state.realName} onChange={(value)=>{this.setState({realName:value})}} />
                    </Form.Item>

                    <Form.Item
                        label='电子邮箱：'
                        rules={[{ required: true, message: '电子邮箱不能为空' }]}
                    >
                        <Input placeholder='请输入电子邮箱' value={this.state.email} onChange={(value)=>{this.setState({email:value})}} />
                    </Form.Item>
                    <Form.Item
                        label='联系电话：'

                    >
                        <Input placeholder='请输入联系电话' value={this.state.phone} onChange={(value)=>{this.setState({phone:value})}} />
                    </Form.Item>
                    <Form.Item
                        label='手机号码：'
                    >
                        <Input placeholder='请输入手机号码' value={this.state.mobilePhone} onChange={(value)=>{this.setState({mobilePhone:value})}} />
                    </Form.Item>
                    <Form.Item
                        label='公司：'
                    >
                        <Input placeholder='请输入公司' value={this.state.company} onChange={(value)=>{this.setState({company:value})}} />
                    </Form.Item>
                    <Form.Item
                        label='所属机构:'
                    >
                        {this.state.departmentName}
                    </Form.Item>
                    <Form.Item
                        label='角色:'
                    >
                        {this.state.roles}
                    </Form.Item>

                </Form>
            </>


        )


    }
}

export default withRouter(Profile)